﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Reflection;
using System.Text;
using System.IO;

namespace DBScript
{
    /// <summary>
    /// Copyright (C) 2009-2012 Xuzhibin
    /// 数据访问接口
    /// 用户可以修改满足自己项目的需要。
    /// Created On 2009-02-04  
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IDBBase
    {
        #region 数据库链接管理
        /// <summary>
        /// 自定义切换数据库
        /// </summary>
        /// <param name="CName"></param>
        /// <returns></returns>
        bool SwitchConnect(string CName);
        /// <summary>
        /// 停止切换，还原为默认方式
        /// </summary>
        /// <returns></returns>
        bool SwitchEnd();
        #endregion 数据库链接管理

        #region Add

        /// <summary>
        /// 添加记录，返回当前插入的这条记录的ID
        /// </summary>
        /// <param name="model">实体层某实体的实例</param>
        int Add<T>(T model) where T : DBModel, new();

        #endregion Add

        #region Delete
        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="id">记录ID</param>
        int Delete<T>(int id) where T : DBModel, new();

        int Delete<T>(long id) where T : DBModel, new();

        /// <summary>
        /// 删除记录
        /// </summary>
        int Delete();

        int Delete<T>() where T : DBModel, new();

        #endregion Delete

        #region Update

        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">需要更新到数据库的实体类</param> 
        bool Update<T>(T model) where T : DBModel, new();
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="field">更新字段</param>
        IDBBase UpdateFields(string fields);

        bool Update();

        #endregion Update

        #region Select
        /// <summary>
        /// 设置Table
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        IDBBase From<T>() where T : DBModel, new();
        IDBBase From(string table,string pkey);

        /// <summary>
        /// 设置SQL
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        IDBBase SetSQL(string sql);

        /// <summary>
        /// 设置Query
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        IDBBase SetQuery(QueryBase query);

        /// <summary>
        /// 设置条件
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        IDBBase Where(string where);

        /// <summary>
        /// 查询字段
        /// </summary>
        /// <param name="columns"></param>
        /// <returns></returns>
        IDBBase Select(string columns);

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="orderby"></param>
        /// <returns></returns>
        IDBBase OrderBy(string orderby);

        /// <summary>
        /// 分组
        /// </summary>
        /// <param name="groupby"></param>
        /// <returns></returns>
        IDBBase GroupBy(string groupby);

        /// <summary>
        /// 分组后查询条件
        /// </summary>
        /// <param name="having"></param>
        /// <returns></returns>
        IDBBase Having(string having);

        /// <summary>
        /// 分页
        /// </summary>
        IDBBase Page(int index,int size);
        IDBBase LimitFirst(int first);
        IDBBase LimitLengh(int lengh);

        #endregion Select

        #region 设置参数
        IDBBase SetString(string name,string value);
        IDBBase SetInt(string name, int value);
        IDBBase SetParam(DBParam pm);
        IDBBase SetListParams(List<DBParam> listPm);
        #endregion 设置参数
       
        #region 返回单个值
        /// <summary>
        /// 最大ID值
        /// </summary>
        int MaxInt(string key);
        /// <summary>
        /// 获得记录数
        /// </summary>
        int GetCount();
        /// <summary>
        /// 获得Int
        /// </summary>
        int GetInt();
        /// <summary>
        /// 获得String
        /// </summary>
        string GetString();
        /// <summary>
        /// 获取object[]列表
        /// </summary>
        /// <returns></returns>
        List<object[]> GetListObjects();
        #endregion 返回单个值

        #region Exists
        /// <summary>
        /// 判断记录是否存在
        /// </summary>
        /// <param name="id">记录ID</param>
        bool Exists(int id);

        bool Exists(long id);

        /// <summary>
        /// 判断记录是否存在
        /// </summary>
        bool Exists();

        #endregion Exists

        #region GetModel

        /// <summary>
        /// 获得一个Model对象实例
        /// </summary>
        /// <param name="id">主键ID的值</param>
        T GetModel<T>(int id) where T : DBModel, new();

        T GetModel<T>(long id) where T : DBModel, new();

        /// <summary>
        /// 获得一个Model对象实例
        /// </summary>
        T GetModel<T>() where T : DBModel, new();

        /// <summary>
        /// 获得一个Model对象实例
        /// </summary>
        T GetModel<T>(IDataReader dr) where T : DBModel, new();

        /// <summary>
        /// 获得一个Model对象实例
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dataRow"></param>
        /// <returns></returns>
        T GetModel<T>(DataRow dataRow) where T : DBModel, new();

        #endregion GetModel

        #region  GetList

        /// <summary>
        /// 获得数据集
        /// </summary>
        /// <returns></returns>
        List<T> GetList<T>() where T : DBModel, new();

        /// <summary>
        /// 获取数据集及总数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        ArrayList GetListAndCount<T>() where T : DBModel, new();

        /// <summary>
        /// 获得List集合
        /// </summary>
        /// <param name="dr">将DataReader里的实体转到List</param>
        List<T> GetList<T>(IDataReader dr) where T : DBModel, new();

        /// <summary>
        /// 获得List集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        List<T> GetList<T>(DataTable dt) where T : DBModel, new();

        #endregion  GetList        

        #region  GetReader

        /// <summary>
        /// 获得数据集
        /// </summary>
        /// <returns></returns>
        IDataReader GetReader();

        IDataReader GetReader<T>() where T : DBModel, new();

        #endregion  GetReader        

        #region 事务

        /// <summary>
        /// 开启事务
        /// </summary>
        void BeginTransaction();

        /// <summary>
        /// 提交事务
        /// </summary>
        void Commit();

        #endregion 事务

        #region 执行SQL语句
        /// <summary>
        /// 执行sql语句并返回单个结果
        /// </summary>
        object ExecuteScalar();
        object ExecuteScalar(QueryType qty);
        /// <summary>
        /// 执行sql语句，返回受影响行数
        /// </summary>
        /// <param name="sqlstr"></param>
        int ExecuteNonQuery();
        int ExecuteNonQuery(QueryType qty);
        /// <summary>
        /// 执行多条SQL语句，实现数据库事务，返回受影响行数。
        /// </summary>
        /// <param name="SQLStringList"></param>
        int ExecuteSqlTran(List<TransactionItem> tranList);
        #endregion
    }
    
    
}
